package cn.iocoder.yudao.module.asasc.controller.admin.profitreport;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.asasc.controller.admin.profitreport.vo.ProfitReportPageReqVO;
import cn.iocoder.yudao.module.asasc.controller.admin.profitreport.vo.ProfitReportRespVO;
import cn.iocoder.yudao.module.asasc.controller.admin.profitreport.vo.ProfitReportSaveReqVO;
import cn.iocoder.yudao.module.asasc.dal.dataobject.profitreport.ProfitReportDO;
import cn.iocoder.yudao.module.asasc.service.profitreport.ProfitReportService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 投入产出报告")
@RestController
@RequestMapping("/asasc/profit-report")
@Validated
public class ProfitReportController {

    @Resource
    private ProfitReportService profitReportService;

    @PostMapping("/create")
    @Operation(summary = "创建投入产出报告")
    @PreAuthorize("@ss.hasPermission('asasc:profit-report:create')")
    public CommonResult<Long> createProfitReport(@Valid @RequestBody ProfitReportSaveReqVO createReqVO) {
        return success(profitReportService.createProfitReport(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新投入产出报告")
    @PreAuthorize("@ss.hasPermission('asasc:profit-report:update')")
    public CommonResult<Boolean> updateProfitReport(@Valid @RequestBody ProfitReportSaveReqVO updateReqVO) {
        profitReportService.updateProfitReport(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除投入产出报告")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('asasc:profit-report:delete')")
    public CommonResult<Boolean> deleteProfitReport(@RequestParam("id") Long id) {
        profitReportService.deleteProfitReport(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得投入产出报告")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('asasc:profit-report:query')")
    public CommonResult<ProfitReportRespVO> getProfitReport(@RequestParam("id") Long id) {
        ProfitReportDO profitReport = profitReportService.getProfitReport(id);
        return success(BeanUtils.toBean(profitReport, ProfitReportRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得投入产出报告分页")
    @PreAuthorize("@ss.hasPermission('asasc:profit-report:query')")
    public CommonResult<PageResult<ProfitReportRespVO>> getProfitReportPage(@Valid ProfitReportPageReqVO pageReqVO) {
        PageResult<ProfitReportDO> pageResult = profitReportService.getProfitReportPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProfitReportRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出投入产出报告 Excel")
    @PreAuthorize("@ss.hasPermission('asasc:profit-report:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProfitReportExcel(@Valid ProfitReportPageReqVO pageReqVO,
                                        HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProfitReportDO> list = profitReportService.getProfitReportPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "投入产出报告.xls", "数据", ProfitReportRespVO.class,
                BeanUtils.toBean(list, ProfitReportRespVO.class));
    }

}